package com.zone.vehicle.superApps.view.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.zone.vehicle.superApps.databinding.FragmentSetCodeBinding;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SetCodeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SetCodeFragment extends Fragment {

    private FragmentSetCodeBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentSetCodeBinding.inflate(inflater);
        init();
        return binding.getRoot();
    }

    private void init(){
        binding.numIn1Et.addTextChangedListener(new TextChangeLister(binding.numIn1Et,binding.numIn2Et));
        binding.numIn2Et.setOnKeyListener(new MyOnKeyListenr(binding.numIn1Et, binding.numIn2Et));
        binding.numIn2Et.addTextChangedListener(new TextChangeLister(binding.numIn2Et,binding.numIn3Et));
        binding.numIn3Et.setOnKeyListener(new MyOnKeyListenr(binding.numIn2Et, binding.numIn3Et));
        binding.numIn3Et.addTextChangedListener(new TextChangeLister(binding.numIn3Et,binding.numIn4Et));
        binding.numIn4Et.setOnKeyListener(new MyOnKeyListenr(binding.numIn3Et, binding.numIn4Et));
    }

    class TextChangeLister implements TextWatcher {
        private EditText thisEt,newxtEt;
        public TextChangeLister(EditText thisEt,EditText newxtEt){
            this.thisEt=thisEt;
            this.newxtEt=newxtEt;
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //text 输入框中改变后的字符串信息
            //start 输入框中改变后的字符串的起始位置
            //before 输入框中改变前的字符串的位置 默认为0
            //count 输入框中改变后的一共输入字符串的数量
            if(before==0&&count==1){
                thisEt.clearFocus();
                newxtEt.requestFocus();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    //自定义键盘监听
    class MyOnKeyListenr implements View.OnKeyListener {
        private EditText upDt, thisDt;

        public MyOnKeyListenr(@Nullable EditText upDt, @Nullable EditText thisDt) {
            this.upDt = upDt;
            this.thisDt = thisDt;

        }

        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            Log.i("键盘监控", "view:" + view + "   i:" + i + "   keyEvent:" + keyEvent.toString());
            if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_DEL) {  //如果上一个输入框不为空
                if (upDt != null && thisDt.getText().toString().isEmpty()) {
                    thisDt.clearFocus();
                    upDt.requestFocus();
                }
                return false;
            }
            return false;
        }
    }
}